home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7059 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  84 lines

  1. Path: in1.uu.net!van-bc!usenet
  2. From: ralphb@wimsey.com
  3. Newsgroups: comp.lang.c
  4. Subject: Assistance needed - pointers to functions
  5. Date: 18 Feb 1996 01:10:30 GMT
  6. Organization: Online at Wimsey
  7. Message-ID: <4g5ua6$f2i@wolfe.wimsey.com>
  8. Reply-To: ralphb@wimsey.com
  9. NNTP-Posting-Host: pm024.vcr.wis.net
  10. X-Newsreader: IBM NewsReader/2 v1.9d - NLS
  11.  
  12. Hi, Folks!  I have a stumbling block regarding pointers to functions,
  13. in that I cannot seem to fathom how to get hold of variables in other
  14. functions.  For example, in the code segment below I would eventually 
  15. like to be able to search student records which have been defined,
  16. and filled in the code seg following this one:
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "ass2.h"
  21.  
  22. int dosection(int selection)
  23. {
  24.    switch(selection)
  25.    {
  26.       case SEARCH:
  27.            /* do search routines here */
  28.            printf("\nSEARCH");
  29.            return 1;
  30.         :
  31.         : (Stuff deleted)
  32.         :
  33.       default:
  34.            return 0;
  35.    }
  36. }
  37.  
  38. Below is the function that fills up a few student records initially.
  39.  
  40. int search_student_record()
  41. {
  42.    FILE *fp;
  43.    STUDENT_RECORD S; /* Define S as a student record */
  44.    int i=0;          /* Index into student record element */
  45.    INDEX student_index[MAX_STUDENT_REC]; /*Define student_index array of type INDEX */
  46.    long f_pos=0;
  47.    fp = fopen("student.dat","r");
  48.    fseek(fp,0,SEEK_SET);
  49.    if (fp == NULL)
  50.       return 1;
  51.    else
  52.    {
  53.       while (!feof(fp))
  54.       {
  55.          f_pos = ftell(fp);
  56.          fread(&S,sizeof(S),1,fp);
  57.  
  58.          student_index[i].f_pos = f_pos;
  59.          student_index[i].id = S.id;
  60.          strcpy(student_index[i].name,S.name);
  61.          i++;
  62.       }
  63.       return 0;
  64.    }
  65. }
  66.  
  67. So I have a array of student_index whose structure is in a header file
  68. somewhere else.  The code compiles perfectly (no errors, no warnings). 
  69.  
  70. Now the question:
  71.  
  72. How do I reference student_index[i] in the first function so that I
  73. can search through it and make modifications, keeping in mind that 
  74. these functions are seperate .c files?
  75.  
  76. I have many such functions where I would need to get to a structure, 
  77. or even a simple int variable all by itself, and use it inside of a
  78. different function, so any help you can provide on this is much 
  79. appreciated.
  80.  
  81. Thanks,
  82. Ralph
  83.  
  84.